package com.SyDeck.sdCities;

/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import org.bukkit.ChatColor;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;

/**
 *
 * @author Deck
 */
public class rpgBlockListener extends BlockListener {

    static HashMap<String, Polygon> poly = new HashMap<String, Polygon>();
    static HashMap<String, ArrayList> Citizens = new HashMap<String, ArrayList>();
    static ArrayList<String> Cities = new ArrayList<String>();
    CityInfo ci = new CityInfo();
    CityPath CP = new CityPath();

    public void buildhash() {
        CityInfo ci = new CityInfo();
        for (int x = 0; x < ci.GetBounds().toArray().length; x++) {
            String imsg = ci.GetBounds().toArray()[x].toString();
            String[] msg = imsg.split(";");
            String[] msg2 = msg[2].split(",");
            Polygon Limites = new Polygon();
            for (int i = 0; i < msg2.length; i = i + 3) {
                Limites.addPoint(Integer.parseInt(msg2[i]), Integer.parseInt(msg2[i + 2]));
            }
            poly.put(msg[0], Limites);
            Citizens.put(msg[0], ci.getCityList(msg[0]));
            Cities.add(msg[0]);
        }
    }

    public void createCity(String City, String Player) {
        City = City.toLowerCase();
        Polygon Limites = new Polygon();
        for (int i = 1; i <= 4; i++) {
            String[] XYZ = ci.GetLimits(City, i).split(",");
            Limites.addPoint(Integer.parseInt(XYZ[0]), Integer.parseInt(XYZ[2]));
        }
        ci.SetCityBounds(City.toLowerCase(), ci.GetLimits(City, 1) + "," + ci.GetLimits(City, 2) + "," + ci.GetLimits(City, 3) + "," + ci.GetLimits(City, 4), Player);
        ArrayList<String> Aux = new ArrayList<String>();
        poly.put(City.toLowerCase(), Limites);
        Cities.add(City.toLowerCase());
        Aux.add(Player);
        Citizens.put(City.toLowerCase(), Aux);
    }

    public boolean izcity(int x, int y) {
        if (!Cities.isEmpty()) {
            for (int z = 0; z < Cities.size(); z++) {
                Polygon aux = poly.get(Cities.get(z));
                if (aux.contains(x, y)) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void onBlockBreak(BlockBreakEvent event) {
        if (event.getBlock().getType().toString().equalsIgnoreCase("SPONGE")) {
            event.setCancelled(true);
            event.getPlayer().sendMessage(ChatColor.GOLD + "Soak up, you can't destroy sponges.");
            return;
        }
        if (!Cities.isEmpty()) {
            for (int z = 0; z < Cities.size(); z++) {
                String City = Cities.get(z);
                if (poly.get(City).contains(event.getBlock().getX(), event.getBlock().getZ()) && (!Citizens.get(City).contains(event.getPlayer().getName()))) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage(ChatColor.GOLD + "Propiety of " + ci.CityNameByBlock(event.getBlock().getX(), event.getBlock().getZ()));
                    return;
                }
            }
        }
        if (CP.contains(event.getBlock().getX() + "," + event.getBlock().getY() + "," + event.getBlock().getZ())) {
            event.setCancelled(true);
            event.getPlayer().sendMessage(ChatColor.RED + "Protected block");
            return;
        }
    }

    @Override
    public void onBlockPlace(BlockPlaceEvent event) {
        String City = ci.GetCityName(event.getPlayer().getName());

        if (!Cities.isEmpty()) {
            for (int z = 0; z < Cities.size(); z++) {
                String Citys = Cities.get(z);
                if ((poly.get(Citys).contains(event.getBlockPlaced().getX(), event.getBlockPlaced().getZ())) && (!Citizens.get(Citys).contains(event.getPlayer().getName()))) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage(ChatColor.GOLD + "Propiety of " + ci.CityNameByBlock(event.getBlock().getX(), event.getBlock().getZ()));
                    return;
                }
            }
        }
        if (CP.contains(event.getBlockAgainst().getX() + "," + event.getBlockAgainst().getY() + "," + event.getBlockAgainst().getZ())) {
            event.setCancelled(true);
            event.getPlayer().sendMessage(ChatColor.RED + "Protected block");
            return;
        }
        if (event.getBlock().getType().toString().equalsIgnoreCase("SPONGE")) {
            if (City != null && ci.GetCityStatus(event.getPlayer().getName()).equalsIgnoreCase("Building")) {
                if (ci.GetCityLimits(City) < 4) {
                    String Inc = "" + (ci.GetCityLimits(City) + 1);
                    event.getPlayer().sendMessage(ChatColor.GOLD + "Setting " + City + " limit: " + ci.GetCityLimits(City));
                    ci.SetCityLimits(City, Inc, event.getBlock().getX() + "," + event.getBlock().getY() + "," + event.getBlock().getZ());
                    if (ci.GetCityLimits(City) == 4) {
                        ci.PolygonFix(City);
                        if (ci.izRect(City, event.getPlayer().getName())) {
                            cEconomy cE = new cEconomy();
                            event.getPlayer().sendMessage(ChatColor.GOLD + "City limits successfuly placed with a " + ci.getXsize(City) + "x" + ci.getYsize(City) + " size.");
                            event.getPlayer().sendMessage(ChatColor.GOLD + "In order to finish your city " + cE.CurrPrice(City) + " will be deducted from your account");
                            event.getPlayer().sendMessage(ChatColor.GOLD + "To accept type /Cowner -b");
                            event.getPlayer().sendMessage(ChatColor.GOLD + "If you don´t like the final result just type /Cowner -can");
                            event.getPlayer().sendMessage(ChatColor.GOLD + "For more information about city owner commands type /Cowner");
                            event.getPlayer().sendMessage(ChatColor.GOLD + "However if the /Cowner -b command isn´t executed in the next 30min it will dissapear");
                        } else {
                            event.getPlayer().sendMessage(ChatColor.RED + "City creation failed, make sure it is a rectangle/square or maybe it was too small.");
                            ci.cancelCity(event.getPlayer());
                        }
                    }
                }
            }
        }
    }
}
